package io.summer.basis.config;

import io.summer.basis.log.LogFilter;
import io.summer.basis.log.LogFilterProperties;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ml
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Slf4j
public class FilterConfig {
  @Bean
  FilterRegistrationBean<LogFilter> logFilterRegistry(LogFilterProperties properties) {
    log.info("register log config: {}", properties);
    final var filterRegistrationBean = new FilterRegistrationBean<LogFilter>();
    filterRegistrationBean.setFilter(new LogFilter(properties));
    filterRegistrationBean.setEnabled(properties.isEnable());
    filterRegistrationBean.setUrlPatterns(properties.getIncludePattern());
    return filterRegistrationBean;
  }
}
